Skip to content

Three trees T6x-a1: the statement stack owns statement handles - #346

Merged
jfrolich merged 2 commits into
ocaml-to-rust-rewritefrom
t6x-a1-statement-stack-handles
Nov 17, 2026
Merged

jfrolich merged 2 commits into
ocaml-to-rust-rewritefrom
t6x-a1-statement-stack-handles

Conversation

@jfrolich

@jfrolich jfrolich commented Nov 17, 2026 •

Copy link
Copy Markdown
Owner

Slice T6x-a1 of the backend rewrite (compiler-rust/docs/THREE_TREES.md 11.4, "T6x-a: the statement stack, after the probe"). The lowering's statement stack owns the statement handles of the interface (JsStmt::S) instead of J rows.

What changes

  • out.rs: StatementStack<B: JsStmt>. The slots hold Slot<B::S>, and held is Vec<Option<B::S>>, where a statement moved out leaves None. Default is written out.
    • push, fill and fill_held take handles.
    • hold_run moves each statement; a hold inside the run is moved entry by entry.
    • take_list(js, a, span) -> B::L counts the run and calls JsStmt::list over the statements moved out of their slots (private take_run). It then kills the run and calls close(a, B::list_id(list)). close is unchanged until T6x-b.
    • drop_empty_blocks(js, a, span) uses is_empty_block and release_stmt.
    • The reads first, first_single_slot, only, last, statements and statements_rev still return rows. They read a statement in place through one private peek, which T6x-a3 deletes.
    • New statement_refs(span) reads the statements of a run in place.
    • Every function of the stack has a cost note.
  • mod.rs: the field is statements: StatementStack<B>. The three pushes of the realisation and the push of complete wrap their row. The two lists that go to finish are B::list_id(..).
  • cond.rs: impl norm::IfLists for StatementStack becomes an implementation for StackLists (the stack and the implementation, 16 bytes), with the same six bodies. T6x-a3 deletes it.
  • Every other caller wraps at the call. A push or fill of B::stmt_node(x) gives x; a row gives B::stmt_from_node(row). A take_list whose result went into B::list_from loses both wraps; a stored or passed-on list is B::list_id(..).
  • args.rs::declares_pure takes &B::S (private, one file) and reads the arguments' runs with statement_refs.

Decision not given by the task

cond.rs has three sites: place, block_statement, and the guard of an if without an else. Each pushes or fills a handle and then notes it as an empty block, and note_empty_block still takes a NodeId. A plain wrap there would put a stmt_node/stmt_from_node pair on one value. Instead, place keeps its row argument. The other two take the noted id from JsStmt::stmt_id before the push; on rows that is the row. The interface documents stmt_id as the key of this table, and T6x-a3 changes the table's key type to it.

Gates

cargo test --manifest-path compiler-rust/Cargo.toml --lib bir::lower:

test result: ok. 568 passed; 0 failed; 0 ignored; 0 measured; 2410 filtered out

cargo test --manifest-path compiler-rust/Cargo.toml --lib bir::j:

test result: ok. 58 passed; 0 failed; 0 ignored; 0 measured; 2920 filtered out

The four allocation counts in unit.rs stay 86, 103, 86, 156, and every size_of keeps its number (StatementStack<Rows> 96, Option<Slot<rows::S>> 12).

cargo check --all-targets and cargo clippy --all-targets -- -D warnings: no output.

RUSTDOCFLAGS="-D warnings" cargo doc --locked --manifest-path compiler-rust/Cargo.toml --all-features --no-deps: no warning.

python3 scripts/check_lowering_cost_docs.py --base origin/ocaml-to-rust-rewrite:

cost docs: production functions=541; changed/new=90; missing/generic=0; removed=0

CI Backend bridge coverage: every *_differences is 0, and "Backend coverage ratchet passed".

CI Instruction count:

| base | 4,135,256,308 |
| head | 4,134,774,295 |
| change | -482,013 (-0.012%) |

Check mode, run locally on two release builds of rewatch (base = origin/ocaml-to-rust-rewrite 61f7f41). Both sides used the same checkout, which --sequential allows; the two commits differ only in compiler-rust:
python3 scripts/compare_build_instructions.py --base base-rescript CHECKOUT --head head-rescript CHECKOUT --sequential

| base | 4,148,590,352 | 4,150.4, 4,151.0, 4,150.6, 4,148.6, 4,151.0 | 788 |
| head | 4,148,357,110 | 4,150.4, 4,149.1, 4,148.4, 4,150.7, 4,149.7 | 788 |
| change | -233,242 (-0.006%) | | |

git status was clean afterwards.

python3 ../scripts/count_lowering_ids.py | grep '^| total', first line, beside the probe's:

this PR: | total | 15643 | 213 | 108 | 585 | 270 | 142 | 68 | 51 | 33 | 14 | 7 | 1 | 5 | 165 |
probe:   | total | 15647 | 213 | 108 | 621 | 270 | 142 | 68 | 54 | 36 | 28 | 23 | 1 | 5 | 165 |

Why the counts differ (36 fewer transition calls):

  • The probe kept 14 B::list_id(take_list(..)) whose list then went straight into B::list_from. Here both wraps go: 14 fewer list_from and 16 fewer list_id, two of those 16 being the B::list_id pairs of if_statement.
  • The three empty-block sites above avoid 3 stmt_from_node + 3 stmt_node.
  • The 4 fewer lines come from those same edits.

End state

$ rg -n 'struct StatementStack<B: JsStmt>' src/bir/lower/out.rs
54:pub struct StatementStack<B: JsStmt> {
$ rg -n 'B::stmt_node\(|B::stmt_from_node\(' src/bir/lower/out.rs   # above `mod tests` (line 672)
93:        let id = B::stmt_node(std::mem::replace(s, B::stmt_from_node(NodeId(u32::MAX))));
94:        *s = B::stmt_from_node(id);
$ rg -n 'B::list_id\(' src/bir/lower/out.rs
476:        self.close(a, B::list_id(list));
$ rg -n 'bir\.(view|release)\(' src/bir/lower/out.rs   # above `mod tests`: empty

The B::stmt_node/B::stmt_from_node search finds the three calls, all in peek, on two lines.

rg -n '(push|fill)\(B::stmt_node\(' src/bir/lower finds no call of the stack. Five lines push onto other Vecs of rows that later slices own:

  • args.rs:88, args.rs:124: Evaluation::statements (T6x-a2)
  • tail.rs:65: tail_assignments (T6x-a2)
  • expr.rs:310: scratch of hold_effect, a hold filled by a sink (T6x-a2)
  • ffi_args.rs:305: the actions of a string switch (T6x-a4)

Tests edited, only to compile

  • out.rs: use crate::bir::j::rows::Rows, StatementStack::<Rows>::default(), Rows::stmt_from_node(..) around pushed and filled rows and inside the expected Slot::Statement(..), take_list(&mut Rows::default(), &mut bir, ..), and the type argument of the two size assertions. This includes the_actions_of_ten_thousand_switches_leave_only_the_slots_of_their_runs.
  • args.rs: one push wrapped. Only one test of the file calls the stack in a way that stops compiling; the other reads it with calls whose signature is unchanged.
  • cond.rs: the four pushes of the Hand helper wrapped.
  • oracle_tests.rs: one push and one take_list.

No expected value changed.

Started by Jaap Frolich in this Assistant session

Why:
    Phase T6x of the three trees removes the J rows the lowering stores
    and passes. Its first row (T6x-a1) puts the statement stack itself on
    the interface: the stack owns the statements of its runs as handles
    of the implementation, so a statement built through the interface is
    pushed as it is, not turned into a row first.

Representation modified (StatementStack):
Before:
    struct StatementStack { slots: Vec<Option<Slot>>, held: Vec<NodeId>, .. }
    enum Slot { Statement(NodeId), Held(Span), Dead(u32) }
    push(NodeId), fill(slot, NodeId), fill_held(slot, &[NodeId])
    take_list(&mut Bir, Span) -> ListId
    drop_empty_blocks(&mut Bir, Span)   // View match, Bir::release
After:
    struct StatementStack<B: JsStmt> { slots: Vec<Option<Slot<B::S>>>, held: Vec<Option<B::S>>, .. }
    enum Slot<S> { Statement(S), Held(Span), Dead(u32) }
    push(B::S), fill(slot, B::S), fill_held(slot, impl IntoIterator<Item = B::S>)
    take_list(&mut B, &mut Pools, Span) -> B::L   // JsStmt::list over the moved run
    drop_empty_blocks(&mut B, &mut Pools, Span)   // is_empty_block, release_stmt

`hold_run` and `take_list` move each statement out of its slot (a hold
inside a run entry by entry). The reads keep their results as rows
(`first`, `first_single_slot`, `only`, `last`, `statements`,
`statements_rev`) and read a statement in place through one private
`peek`, which T6x-a3 deletes. `close` keeps its signature (T6x-b), so
`take_list` gives it `B::list_id` of the list.

Added, each with its cost in its doc comment: `statement_refs` (the
statements of a run read in place; O(slots + held statements));
`take_run` (private: the statements of a run moved out; same cost);
`slot_len` (O(1)) and `slot_row` (O(1), through `peek`) in place of
`slot`; `peek` (O(1)); `Default` written out (O(1)). In `cond.rs` the
implementation of `norm::IfLists` moves from the stack to `StackLists`,
the stack and the implementation (16 bytes), with the same six bodies;
T6x-a3 deletes it.

Callers wrap at the call: a push or fill of `B::stmt_node(x)` gives `x`;
of a row, `B::stmt_from_node(row)`; a `take_list` whose result went into
`B::list_from` loses both wraps, one that is stored or passed on as a
`ListId` is `B::list_id(..)`. `args.rs::declares_pure` takes `&B::S` and
reads the runs of the arguments with `statement_refs`. Where a caller
pushed a handle and then noted it as an empty block, the row it notes is
`JsStmt::stmt_id` of the handle, read before the push (on rows the row),
so no `stmt_node`/`stmt_from_node` pair is left: `cond.rs` `place`,
`block_statement` and the guard of an `if` without an else.

Generated JavaScript does not change; the default build does not run
this code.

Tests: `--lib bir::lower` 568 passed, `--lib bir::j` 58 passed, none
ignored. The tests of the stack and of its callers changed only to
compile (`StatementStack::<Rows>`, `Rows::stmt_from_node` around a pushed
row, `take_list` with an implementation); no expected value changed; the
two size assertions keep 96 and 12.

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-6e0f5c7f-c4b3-74aa-868a-9195e58e2297
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
Internal release note for the statement stack of the backend lowering
on statement handles (three trees, T6x-a1).

Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-6e0f5c7f-c4b3-74aa-868a-9195e58e2297
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
@jfrolich
jfrolich marked this pull request as ready for review November 17, 2026 21:59
@jfrolich

Copy link
Copy Markdown
Owner Author

First-pass checklist review at 36d7b91 (a checklist for the final reviewer, not an approval).

First-pass review (checklist, not an approval):

Setup: git fetch origin; D="origin/ocaml-to-rust-rewrite...origin/t6x-a1-statement-stack-handles"; git diff $D > $TMPDIR/pr.diff (1559 lines). Merge base = trunk tip = 61f7f41292. Two commits: b0ca375bb8 (stack and callers), 36d7b9107c (changelog fragment), in the order the task asks. Task read on trunk from compiler-rust/docs/THREE_TREES.md: "T6x-a: the statement stack, after the probe" (l. 4306), "Common to every prompt of phase C" (l. 5203), "Common to T6x-a1 … a4" (l. 6260), "Task T6x-a1:" (l. 6341). Files on the branch were read with git show origin/t6x-a1-statement-stack-handles:PATH. Nothing was checked out, built or pushed.

1. Scope — PASS (one task end-state line is not met; see 18)

Task item Where (branch)
enum Slot<S>, StatementStack<B: JsStmt>, held: Vec<Option<B::S>>, counters/empties as they were out.rs:43-66
Default written out out.rs:68-73
imports super::js::Pools, super::js_stmt::JsStmt out.rs:2-3
push(B::S), fill(slot, B::S) out.rs:367, 380
fill_held(slot, impl IntoIterator<Item = B::S>) out.rs:389
hold_run moves (Option::take; a hold is moved entry by entry) out.rs:402-428
take_list(js, a, span) -> B::L, private take_run out.rs:464-478, 193-215
drop_empty_blocks(js, a, span) on is_empty_block/release_stmt out.rs:335-348
reads keep row results through one peek out.rs:92-96; first 255, first_single_slot 261, only 272, last 282, statements 144, statements_rev 170
statement_refs out.rs:118
cost note on every function of the stack see 12
callers wrap at the call see 16
cond.rs StackLists cond.rs:67-94
mod.rs field and six calls mod.rs:491, 809, 825, 830, 1153, 1158, 1163
args.rs::declares_pure(&B::S) args.rs:32
test edits only to compile see 20
commit 2: changelog fragment changes/statement-stack-on-handles.internal.md
end state 4 of 5 checks hold; `(push

Not done, by the body's own account: the five push(B::stmt_node(..)) lines (section 18); place keeps its NodeId argument (section 17).
Done but not asked: private helpers slot_len (out.rs:218) and slot_row (out.rs:227) replace the old slot(at) -> &[NodeId]. The commit message names both, and the common block allows private helpers. mod.rs:1143 adds a doc line to leaf (/// O(1): at most one statement built and pushed.). The task's "the push of seq" and "into the comma" do not match the code: seq (mod.rs:1176) pushes nothing, and both lists go to finish (from complete_unit :809 and complete :830). The builder converted the push in complete (:825). That reading is correct.

2. Default path — PASS

Every changed function belongs to bir::lower. The lowering runs only under BackendMode::Check (compiler-rust/src/bir/shadow.rs:24-34: checks_lowering is matches!(self, Self::Check), and from_env maps RESCRIPT_BACKEND=check). No function on an ordinary build changed. CI Instruction count on the default build: | change | -482,013 (-0.012%) |. The body also reports a check-mode measurement (section 22).

3. Engineering standard — PASS, with notes on (b)

Greps over the added lines of $TMPDIR/pr.diff:

  • (a) Every new or changed function above mod tests has a cost note (table in 12). CI script-tests prints cost docs: production functions=541; changed/new=90; missing/generic=0; removed=0.
  • (b) New loops: statement_refs, take_run, the rewritten statements/statements_rev, and the inner per-hold loop of hold_run. The existing 10,000 tests cover take_list over 10,001 slots: a_statement_is_copied_once asserts len 10,001, first = NodeId(10_000), pushed == 10_001, copied == 10_001, empty stack. The other is the_actions_of_ten_thousand_switches_leave_only_the_slots_of_their_runs. No test at 10,000 drives a Held slot through take_run, statement_refs or hold_run. The task asks for no new test ("Tests you edit, only so that they compile"), so this is UNCLEAR against the general rule, not a FAIL against the task.
  • (c) HashMap/HashSet/BTreeMap added: none.
  • (d) Allocation in a walk: grep -nE '^\+' pr.diff | grep -E 'Vec::new|vec!|String|format!|Box::new|Rc::|Arc::|SmallVec|\.clone\(\)|to_vec|collect' finds out.rs:70 Vec::new() ×3 in Default (no allocation) and a test line (vec![Some(Slot::Statement(..))], out.rs tests). Nothing else.
  • (e) lock/atomic/thread_local/RefCell: none.
  • (f) Second pass: take_list counts the run with statement_refs(span).count() and then moves it with take_run: two passes over the run. The old code made the same two (statements(span).count() then push_iter), the task prescribes it ("counts the statements of the run, calls js.list(a, count, ..)"), and the cost note states both passes.
  • (g) New per-node type Slot<S>: size_of::<Option<Slot<<Rows as JsStmt>::S>>>() == 12 (out.rs tests). StatementStack<Rows> 96. StackLists (16 bytes per its doc) has no size test; it is built once per if_in_place and passed by &mut, not stored per node.
  • (h) allow(, unsafe, todo!, unimplemented!, #[ignore]: none added.

4. Tests and oracles — PASS

The diff does not touch backend_coverage.txt, printer_coverage.json, tc_coverage.txt, any script or any expected file (git diff --name-only $D | grep -E "tests/|coverage|scripts/|\.github" returns nothing). The test hunks only wrap or add a type argument (section 20).

5. New scripts and fixtures — PASS (none added)

6. Privacy — PASS

grep -n -i -E "tella|submodule\.nl|rescript-bench-corpus|/Users/|opensession|jake" $TMPDIR/pr.diff: no output (exit 1). The commit messages carry only the Session: trailer URL.

7. Changelog — PASS

changes/statement-stack-on-handles.internal.md: - The backend lowering's statement stack owns statement handles of its JavaScript interface instead of J rows. CHANGELOG.md is untouched.

8. Numbers — PASS

Each figure in the body comes with its command and pasted output: the test result lines, the cost-docs line, the CI Instruction count table, the check-mode table with its command, and the count_lowering_ids totals. One exception: the allocation counts 86/103/86/156 are stated without output. unit.rs is not in the diff, and CI ran all four full_unit_allocation_boundary_* tests ... ok. Verified figures are in section 22.

9. Focused tests — not run (host review)

CI compiler-tests log of this head: grep -c 'test bir::lower::.* ... ok' = 568, ignored 0; bir::j = 58, ignored 0. The whole run reads test result: ok. 2954 passed; 0 failed; 24 ignored. All 7 bir::lower::out::tests::* pass.

10. Where I would look hardest

  1. Mixed keys in the empty-block table (cond.rs:372-379, :506-514, :724-731; out.rs:341). place notes the row it was given, while block_statement and the if guard in free_effect note NodeId(self.js.stmt_id(..)). drop_empty_blocks compares the entry with peek(held), a row. On Rows the two are the same (rows_stmt.rs stmt_id = s.0.0), but no other implementation can use the table until T6x-a3 rekeys it. This decision is the builder's, not the task's. It also adds NodeId(<u32 from stmt_id>), an id cast that none of the four transition greps finds.
  2. Row round trips through Vec<NodeId> fields at expr.rs:310-311 (held.push(B::stmt_node(s)), then fill_held(slot, held.iter().map(|&row| B::stmt_from_node(row)))) and tail.rs:65/67 (assignments.push(B::stmt_node(..)), then self.statements.push(B::stmt_from_node(statement))). The function writes the row into a vector and wraps it again on the next line. The task's fill_held(slot, &rows) rule produces exactly this, and scratch/tail_assignments are Vec<NodeId> State fields (mod.rs:449, :546) that only T6x-a2 may retype. It is still the turn-back pair the common block names, and it contradicts the task's end-state line (section 18).
  3. take_run / hold_run on held slots (out.rs:193-215, 402-428). A Held slot is take()n and left a hole while its entries become None. Correctness rests on kill running right after and on js.list consuming exactly count items. No test drives a hold through take_list at scale.

11. Files and signatures — PASS

git diff --stat $D: 20 files. Every file is under compiler-rust/src/bir/lower/ except changes/statement-stack-on-handles.internal.md. js.rs, js_stmt.rs and everything under src/bir/j are untouched (git diff --name-only $D | grep -E 'js\.rs|js_stmt\.rs|src/bir/j/' returns nothing).
Changed fn lines above mod tests outside out.rs:

args.rs: -    fn declares_pure(&self, statement: NodeId) -> bool {
args.rs: +    fn declares_pure(&self, statement: &B::S) -> bool {
cond.rs: -    fn trim(&mut self, b: &mut Bir, span: Span) { self.drop_empty_blocks(b, span); }
cond.rs: +    fn trim(&mut self, b: &mut Bir, span: Span) { self.stack.drop_empty_blocks(self.js, b, span); }
cond.rs: -    fn last(&mut self, _: &Bir, span: Span) -> Option<NodeId> { self.last(span) }
cond.rs: +    fn last(&mut self, _: &Bir, span: Span) -> Option<NodeId> { self.stack.last(span) }
cond.rs: -    fn take(&mut self, b: &mut Bir, span: Span) -> ListId { self.take_list(b, span) }
cond.rs: +    fn take(&mut self, b: &mut Bir, span: Span) -> ListId { B::list_id(self.stack.take_list(self.js, b, span)) }

The cond.rs lines are the IfLists bodies moved to StackLists; their trait signatures are unchanged. declares_pure is the one signature change the task allows. mod.rs field change: - statements: StatementStack, / + statements: StatementStack<B>,. No other field of State or of a plan changes.

12. out.rs, function by function — PASS (two minor note gaps, both copied from trunk)

fn old sig new sig in task statement handling cost note (quoted) / true?
Span::end unchanged unchanged – none "endpoint of a run (no tree walk)" ✓
Default::default derived written out yes none "O(1); allocates nothing" ✓ (Vec::new)
with_capacity (usize)->Self same – none "Reserves O(N) …" ✓
reserve_repeat, capacities #[cfg(test)] same – none none (test-only)
peek – (&mut B::S)->NodeId yes, verbatim mem::replace with placeholder, then put back "O(1)" ✓
mark / since same same yes none "O(1)" ✓
statement_refs – (&self, Span)->impl Iterator<&B::S> yes read by reference "O(slots of the run + held statements)" ✓
statements (&self, Span)->Iter (&mut self, Span)->Iter yes (&mut self) read through peek "O(slots + held), each … through peek" ✓
statements_rev (&self)… (&mut self)… yes peek "one step per slot and per held statement" ✓
take_run – private (&mut self, Span)->Iter<B::S> yes Option::take of slot / held entry; Dead/None put back "O(slots + held), each moved once" ✓
slot (old) (&self, usize)->&[NodeId] deleted – – –
slot_len – (&self, usize)->usize private helper none "O(1)" ✓
slot_row – (&mut self, usize, usize)->NodeId private helper peek "O(1)" ✓
live same same – none "One step per hole, empty hold or dead range" ✓
first same same yes via slot_row/peek "Costs the holes and dead ranges before the first statement" ✓
first_single_slot same same yes slot_len==1, slot_row "O(leading holes/dead ranges)" ✓
only same same yes two live scans, slot_row "Costs the holes and dead ranges of the run" ✓
last same same yes slot_row(at, len-1) "One step on a trimmed run; otherwise one per trailing hole or dead slot" ✓
trim same same yes slot_len "One step per popped slot" ✓
kill same same yes overwrites/truncates "One write per slot of the run" ✓
note_empty_block same same yes none "O(log E) … and an insertion": the Vec::insert shift is O(E) and the note leaves it out. Trunk's note and the design table do the same, so this is minor.
drop_empty_blocks (&mut Bir, Span) (&mut B, &mut Pools, Span) yes compared via peek(held) == statement, then slot.replace(Dead(1)) moves it out, release_stmt "two binary searches … one step per empty block … The run itself is not scanned". drain(from..to) also shifts the tail of empties, unmentioned, as on trunk. Minor.
clear same same yes overwrite "One write" ✓
has_statements same same (now via statement_refs) yes by reference "O(holes and dead ranges before the first statement)" ✓
remove_empty same same yes none "O(slots of the run)" ✓
push (NodeId) (B::S) yes moved in "One write" ✓
reserve same same yes none "One write" ✓
fill (u32, NodeId) (u32, B::S) yes moved in "One write" ✓
fill_held (u32, &[NodeId]) (u32, impl IntoIterator<Item=B::S>) yes moved, Some each "one step per statement, each moved once" ✓
hold_run same same yes slots[at].take(); held entries take() one by one "one step per slot of the run and per statement of a hold" ✓
append same same yes truncate "O(1), and one write per slot of the second run when a finished first run pops it" ✓
take_list (&mut Bir, Span)->ListId (&mut B, &mut Pools, Span)->B::L yes count by ref, move via take_run "one step per statement, hole and dead range … to count them, and one per slot and statement to move them" ✓
close same same yes (unchanged) none "that of close::decide" ✓

13. peek — PASS

    /// The row of a statement the stack holds, the handle staying
    /// in place. O(1). Transition: deleted by T6x-a3.
    fn peek(s: &mut B::S) -> NodeId {
        let id = B::stmt_node(std::mem::replace(s, B::stmt_from_node(NodeId(u32::MAX))));
        *s = B::stmt_from_node(id);
        id
    }

This is the task's text verbatim. Callers (out.rs): statements :150, :161; statements_rev :176, :183; slot_row :229, :232; drop_empty_blocks :341.
git grep -n -E 'B::stmt_node\(|B::stmt_from_node\(' origin/t6x-a1-statement-stack-handles -- compiler-rust/src/bir/lower/out.rs, above line 672:

out.rs:93:        let id = B::stmt_node(std::mem::replace(s, B::stmt_from_node(NodeId(u32::MAX))));   [peek]
out.rs:94:        *s = B::stmt_from_node(id);                                                          [peek]

That makes three matches on two lines, all in peek. B::list_id(: out.rs:476: self.close(a, B::list_id(list)); [take_list]. bir\.(view|release)\( in out.rs: no output.

14. take_list, take_run, hold_run, fill_held, drop_empty_blocks, kill — PASS

(Bodies in full: base out.rs:343-357 / 288-312 / 277-283 / 229-240 / 208-219; branch out.rs:464-478 / 193-215 / 402-428 / 389-395 / 335-348 / 312-323. kill is unchanged apart from its note.)

  • Old take_list: count = self.statements(span).count(); id = bir.lists.push_iter(count, self.statements(span)); self.copied += count; self.kill(span); self.close(bir, id). New: count = self.statement_refs(span).count(); list = js.list(a, count as u32, self.take_run(span)); self.copied += count; self.kill(span); self.close(a, B::list_id(list)). The order is the same. On rows, js.list is a.lists.push_iter(len, statements.map(|s| s.0)) (rows_stmt.rs:139-142), so it builds the same rows in the same order. take_run walks slots forward and gives a Held run's entries in index order, as the old statements did. The empty-span early return is kept (js.list(a, 0, empty), no close), as before.
  • Per call, nothing is collected: the added-line grep for collect|Vec::new|vec!|to_vec|clone() finds only Default and a test.
  • Counters, old vs new: push: self.pushed += 1 both. fill: self.pushed += 1 both. fill_held: old self.pushed += statements.len();, new self.pushed += len; (len = entries extended), the same number. take_list: self.copied += count; both. scanned: self.scanned += 1; in live, last and trim, at the same places, both sides. hold_run: no counter on either side.
  • hold_run: old held.push(node) / held.extend_from_within(span); new held.push(Some(statement)) / a loop of held[i].take() then held.push(..), in order.
  • drop_empty_blocks: the same partition_point bounds, the same drain(from..to) order, and the same test. Old: *slot == Some(Slot::Statement(statement)) && matches!(bir.view(statement), View::SBlock{statements} if statements.is_empty()). New: peek(held) == statement && js.is_empty_block(a, held), where rows is_empty_block is that same matches! (rows_stmt.rs:293-295). Release goes from bir.release(statement) to js.release_stmt(a, held), which is a.release(s.0) plus steps (rows_stmt.rs:358-360). The same blocks are released in the same order.

15. Steps — PASS

No expected step number changed in the diff (grep -n steps $TMPDIR/pr.diff matches only context lines with primitive_steps/rec_steps). The lowering's tests never read the Rows step counter across a take_list. .steps() is read only in js_tests.rs/js_stmt_tests.rs, on their own Rows values (git grep -n -E "take_list|StatementStack" -- js_tests.rs js_stmt_tests.rs returns nothing). The other steps() readers in bir/lower are names.rs (Replayed) and mod.rs:883 name_replay_steps, which have nothing to do with the stack. Out.rs tests pass a fresh &mut Rows::default() to take_list and assert only list length and pushed/copied. CI Backend bridge coverage (statements_copied is in its report) reports all *_differences: 0 and "Backend coverage ratchet passed".

16. Callers — PASS (with the two staged round trips of section 10.2)

By class, above mod tests:

  • (a) argument lost B::stmt_node: 47 removed lines (grep -c -E '^-.*(push|fill)\(.*B::stmt_node\(|^-\s*&\[B::stmt_node' pr.diff = 47). Every one is a call of the stack (statements.push/fill(..), or the &[B::stmt_node(statement)] argument of fill_held at external.rs:34 and ffi_args.rs:100, now [statement]), in assign, cond, exit, export, expr, external, ffi_args, loops, prim, rec, switch, tail, try_, untagged and variant.
  • (b) row gained B::stmt_from_node: cond.rs:994 (assignment_statement result); exit.rs:423 and loops.rs:156 (if_statement result); mod.rs:825, 1153, 1158, 1163; rec.rs:175 (stored placeholder); tail.rs:67 (tail_assignments row).
  • (c) fill_held with wrapped rows in order: args.rs:206 (…iter().map(|&row| B::stmt_from_node(row))) and expr.rs:311.
  • (d) take_list gets &mut self.js, and its result is used directly where it went into B::list_from: cond.rs:473-476 (×4) with if_(.., yes, no) :479; cond.rs:506, 834, 840; exit.rs:348; expr.rs:283; loops.rs:158; switch.rs:717; tail.rs:109; untagged.rs:648, 796, 874, 955. Stored or passed on gets B::list_id(..): loops.rs:86, mod.rs:809, 830, rec.rs:168, try_.rs:95, cond.rs StackLists::take. drop_empty_blocks(&mut self.js, ..): cond.rs:470-471 and StackLists::trim.
  • (e) other: args.rs:32-38 declares_pure(&B::S) (allowed); args.rs:43 self.declares_pure(&B::stmt_from_node(statement)) (the wrap moves from callee to caller over Evaluation::statements: Vec<NodeId>); args.rs:202 statement_refs (named by the task); cond.rs:67-94 StackLists (section 17); cond.rs:375, 509-514, 727-731 (the empty-block decision, section 17); cond.rs:486 let mut lists = StackLists {..}; mod.rs:1143 doc line. None of these changes what is built, the order, or a signature beyond what the task names.
  • Pair grep over added lines: grep -nE '^\+.*(stmt_from_node\(B::stmt_node|stmt_node\(B::stmt_from_node|list_from\(B::list_id|list_id\(B::list_from)' returns nothing. Across adjacent lines of one function, expr.rs:310→311 and tail.rs:65→67 go row→Vec<NodeId>→handle (section 10.2). The task's rules produce them because those vectors are other slices' fields. The cross-function returns of if_statement/assignment_statement (B::stmt_node in the callee, B::stmt_from_node at exit.rs:423, loops.rs:156, cond.rs:994) are scheduled for T6x-a3 by 11.4 (b).

17. cond.rs — PASS

Struct:

struct StackLists<'s, B: super::js_stmt::JsStmt> {
    stack: &'s mut super::out::StatementStack<B>,
    js: &'s mut B,
}

The six bodies, old → new:

  • trim: self.drop_empty_blocks(b, span) → self.stack.drop_empty_blocks(self.js, b, span). Same work (section 14).
  • empty: EMPTY → EMPTY. Unchanged.
  • last: self.last(span) → self.stack.last(span). Same.
  • only: self.only(span).map(|(_, s)| s) → self.stack.only(span).map(..). Same.
  • release: for statement in self.statements(span) { b.release(statement); } self.kill(span); → for statement in self.stack.statements(span) { b.release(statement); } self.stack.kill(span);. Same rows, same forward order, same kill. It still calls b.release in cond.rs; the task says "the same six bodies", and T6x-a3 deletes this.
  • take: self.take_list(b, span) → B::list_id(self.stack.take_list(self.js, b, span)). Same list (section 14).

The three empty-block sites, old → new:

  • place (cond.rs:372-379). Old: let handle = B::stmt_from_node(statement); let empty = …is_empty_block(&handle); let statement = B::stmt_node(handle); fill(slot, statement); if empty { note_empty_block(slot, statement) }. New: …; fill(slot, handle); if empty { note_empty_block(slot, statement) }. The noted id is the same row (the parameter), the test is on the same statement before the fill, and the pair is avoided. Signature on base cond.rs:364 and branch cond.rs:372: pub(super) fn place(&mut self, slot: u32, statement: NodeId), unchanged.
  • block_statement (cond.rs:501-515). Old: empty = is_empty_block(&block); let block = B::stmt_node(block); push(block); if empty { note(head, block) }. New: let empty = …is_empty_block(&block).then(|| NodeId(self.js.stmt_id(&self.arena, &block))); push(block); if let Some(block) = empty { note(head, block) }. Same moment and same statement; the id is the row on Rows (stmt_id = s.0.0); the pair is avoided.
  • guard of an if without an else, in free_effect (cond.rs:724-732). Same transformation as block_statement; the slot is mark().start before the push, as before.
    On Rows all three note the same row as before. The caveat is section 10.1: the table is now fed by two producers, row and stmt_id.

18. The five (push|fill)\(B::stmt_node\( lines — UNCLEAR (the task's end state says "empty"; the lines are not the stack's)

git grep -n -E '(push|fill)\(B::stmt_node\(' origin/t6x-a1-statement-stack-handles -- compiler-rust/src/bir/lower:

  • args.rs:88 statements.push(B::stmt_node(statement)); and args.rs:124 statements.push(B::stmt_node(statement));. statements is bound at args.rs:67 let Evaluation { work, values, statements } = &mut evaluation;, i.e. Evaluation::statements (Vec<NodeId>).
  • tail.rs:65 assignments.push(B::stmt_node(assignment));. assignments is std::mem::take(&mut self.tail_assignments) (tail.rs:44; tail_assignments: Vec<NodeId>, mod.rs:449). Line 67 then wraps each row back onto the stack.
  • expr.rs:310 self.js.effect(.., |s| held.push(B::stmt_node(s)));. held is std::mem::take(&mut self.scratch) (scratch: Vec<NodeId>, mod.rs:546). Line 311 wraps each row back for fill_held.
  • ffi_args.rs:305 actions.push(B::stmt_node(action));. actions is std::mem::take(&mut self.scratch) (ffi_args.rs:292).
    None of the five calls the stack's push or fill; each pushes into a Vec<NodeId> State field that the task forbids retyping in this slice ("a field of a plan or of State other than statements that changes its type" counts as failure). The lines match the builder's description. Two of them (expr.rs, tail.rs) are the round trips of section 10.2. The final reviewer should decide whether the task's "empty" end state was meant to cover these.

19. mod.rs — PASS

Every changed line:

-    statements: StatementStack,
+    statements: StatementStack<B>,
-                self.statements.take_list(&mut self.arena, run),
+                B::list_id(self.statements.take_list(&mut self.js, &mut self.arena, run)),      [complete_unit → finish]
-        self.statements.push(value);
+        self.statements.push(B::stmt_from_node(value));                                         [complete]
-        let list = self.statements.take_list(&mut self.arena, run);
+        let list = B::list_id(self.statements.take_list(&mut self.js, &mut self.arena, run));  [complete → finish]
+    /// O(1): at most one statement built and pushed.                                         [leaf doc]
-                self.statements.push(stmt);   (×3, Declare / Assign / Return in leaf)
+                self.statements.push(B::stmt_from_node(stmt));

The added line not named by the task is the leaf doc line. The task's "push of seq" is the push in complete, and "into the comma" has no counterpart in the code (section 1).

20. Tests — PASS

Changed lines below mod tests and in oracle_tests.rs:

  • out.rs: + use crate::bir::j::rows::Rows;. StatementStack::default() → StatementStack::<Rows>::default() ×4. Pushed/filled NodeId(n)/next() → Rows::stmt_from_node(..) (in a_statement_is_copied_once, switches_with_actions_on_the_stack, append_* tests). The expected value vec![Some(Slot::Statement(NodeId(0)))] → vec![Some(Slot::Statement(Rows::stmt_from_node(NodeId(0))))]. take_list(&mut bir, ..) → take_list(&mut Rows::default(), &mut bir, ..) ×4.
  • Size assertions: - assert_eq!(size_of::<StatementStack>(), 96); / + assert_eq!(size_of::<StatementStack<Rows>>(), 96); and - assert_eq!(size_of::<Option<Slot>>(), 12); / + assert_eq!(size_of::<Option<Slot<<Rows as JsStmt>::S>>>(), 12);. They still read 96 and 12.
  • args.rs: one push wrapped with <crate::bir::j::rows::Rows as crate::bir::lower::js_stmt::JsStmt>::stmt_from_node(statement). The task said "two tests"; the body explains that only one stops compiling.
  • cond.rs Hand and its neighbours: four push(statement) → push(<Rows as JsStmt>::stmt_from_node(statement)) (diff hunks at 1649, 1687, 1730, 1761). One is in Hand's helper and the others are in tests of the same module; all are wraps only.
  • oracle_tests.rs: one push wrapped; one take_list → <Rows as JsStmt>::list_id(state.statements.take_list(&mut state.js, &mut state.arena, run)).
    No expected value changed, no assertion or test was deleted, and no #[ignore] was added. CI shows 568 bir::lower ok and 0 ignored.

21. Counts as failure — PASS

Added lines above mod tests, grep allow\(|unsafe|MaybeUninit|HashMap|HashSet|Box::new|format!|View::|Op::|plain::|bir\.view\(|bir\.release\(: no hits. Clone|Copy|Default on added lines: only +impl<B: JsStmt> Default for StatementStack<B> { (out.rs:68). There is no bound on B::S or B::L; type L: Copy is the trait's own (js_stmt.rs:102, unchanged). The Default impl as written:

impl<B: JsStmt> Default for StatementStack<B> {
    /// An empty stack without capacity. O(1); allocates nothing.
    fn default() -> Self {
        Self { slots: Vec::new(), held: Vec::new(), pushed: 0, copied: 0, scanned: 0, empties: Vec::new() }
    }
}

git grep -n -E 'bir\.(view|release)\(' origin/t6x-a1-statement-stack-handles -- compiler-rust/src/bir/lower/out.rs returns nothing. Outside out.rs, b.release( stays in StackLists::release (cond.rs), and mod.rs:772 self.arena.release(statement) is an unchanged line. Placeholder in a slot or field: none; held uses Option. The NodeId(u32::MAX) placeholder exists only inside peek, which the task prescribes.

22. Numbers — PASS

  • gh pr checks 346 -R jfrolich/rescript-rust: all 23 pass (Backend bridge coverage, Derived code check, Playground wasm check, Signature equality, Snapshot equivalence, base, build, build-system-tests, build-system-tests (macOS), compare, compiler-tests, compiler-warnings, head, ocaml, oxc parity ×3, parity ×5, script-tests). The PR is MERGEABLE.
  • Instruction count (compare job log, run 44182674129):
| base | 4,135,256,308 | 4,137.4, 4,137.6, 4,135.3, 4,136.3, 4,135.5 | 788 |
| head | 4,134,774,295 | 4,135.9, 4,135.7, 4,134.8, 4,136.3, 4,135.9 | 788 |
| change | -482,013 (-0.012%) | | |

This matches the body and is within +0.02%.

  • Check mode, from the body (local, not reproducible here). Command: python3 scripts/compare_build_instructions.py --base base-rescript CHECKOUT --head head-rescript CHECKOUT --sequential. Five runs per side are shown:
| base | 4,148,590,352 | 4,150.4, 4,151.0, 4,150.6, 4,148.6, 4,151.0 | 788 |
| head | 4,148,357,110 | 4,150.4, 4,149.1, 4,148.4, 4,150.7, 4,149.7 | 788 |
| change | -233,242 (-0.006%) | | |

The body states git status was clean afterwards. The command line does not show how RESCRIPT_BACKEND=check was set ("its default build"). I could not verify it.

  • count_lowering_ids. I reproduced it without cargo: git archive origin/<ref> compiler-rust/src/bir/lower scripts/count_lowering_ids.py | tar -x, then python3 -I ../scripts/count_lowering_ids.py | grep '^| total' from compiler-rust:
branch: | total | 15643 | 213 | 108 | 585 | 270 | 142 | 68 | 51 | 33 | 14 | 7 | 1 | 5 | 165 |   (= body)
trunk:  | total | 15513 | 217 | 105 | 628 | 270 | 142 | 68 | 38 | 82 | 28 | 0 | 1 | 5 | 165 |   (= task's trunk figures 628/38/82/28/0)
probe (task): | total | 15647 | 213 | 108 | 621 | 270 | 142 | 68 | 54 | 36 | 28 | 23 | 1 | 5 | 165 |

Against the probe: stmt_from_node −3, stmt_node −3, list_from −14, list_id −16, total −36, lines −4. From the diff, the 14 list_from the PR drops are grep '^-' pr.diff | grep -o 'B::list_from(' | wc -l = 14: cond.rs if_statement ×2 on one line, block_statement, the two block_of in else_/list near :834/:840; exit.rs:348; expr.rs:283; loops.rs:160; switch.rs:718; tail.rs:111; untagged.rs:651, 797, 876, 956. With 7 B::list_id left (cond.rs take, loops.rs:86, mod.rs:809, 830, rec.rs:168, try_.rs:95, out.rs:476), the probe's 23 = 7 + 16 is consistent with 4 list_id at if_statement's four take_list calls (two per branch of the if yes.start >= no.start) plus 12 at the other sites. The 3+3 statement pairs are the three empty-block sites of section 17. The body's explanation agrees with the diff as far as it can be traced without the probe's file.

  • Test counts 568/58: confirmed from the CI compiler-tests log (568 bir::lower ok, 58 bir::j ok, 0 ignored). Allocation counts 86/103/86/156: unit.rs is not in the diff and its four full_unit_allocation_boundary_* tests pass in CI. Cost-note line: confirmed in the CI script-tests log, cost docs: production functions=541; changed/new=90; missing/generic=0; removed=0.

@jfrolich

Copy link
Copy Markdown
Owner Author

Coordinator review, final, at 36d7b91 (the author did not write this review). The first-pass checklist (comment 6962399373) was read whole; I read out.rs above its tests whole on the branch, the whole diff of cond.rs, and every changed line above the tests of the other eighteen files.

Verdict: accepted. Slice T6x-a1 is what its task describes: the statement stack owns statement handles, every caller hands it one, and nothing that is built, or the order it is built in, changes.

What I checked myself

  • out.rs. Slot<S>, held: Vec<Option<B::S>>, Default written out. peek is the task's body and is the only place of the file that names a transition call for statements (three calls on two lines); take_list holds the one B::list_id, for close. take_run moves each statement once in slot order, puts a dead range or a hole back as it found it, and take_list kills the run right after, so a slot emptied by the move never outlives the call. hold_run moves the entries of an inner hold in order and leaves None behind. drop_empty_blocks makes the same test on the same slot and releases through release_stmt. The counters pushed, copied and scanned advance where they did. Two passes over a run in take_list (count, then move), as on trunk.
  • Callers. The 47 removed B::stmt_node(..) are all arguments of the stack's push, fill or fill_held; a row that comes from a function this slice may not change is wrapped at the push (if_statement and assignment_statement results, the stored recursive placeholder, the three realisations and the seq of mod.rs, the rows of tail_assignments). A taken list goes straight into the constructor where it was wrapped before, and is turned back with B::list_id only where it is stored (loops, rec, try_, mod.rs) or given to norm::IfLists. No stmt_from_node(stmt_node(..)) or list_from(list_id(..)) on any added line.
  • Signatures. Outside out.rs the one change is args.rs::declares_pure(&B::S), which the task names. place keeps its row parameter. No field of State other than statements changed its type.
  • cond.rs. StackLists holds the stack and the implementation, 16 bytes, with the six bodies as they were.
  • Tests. Only wraps; the two size assertions still read 96 and 12; no expected value, assertion or test removed.
  • CI 23 of 23. Instruction count -0.012% (4,135,256,308 to 4,134,774,295). Backend bridge coverage at zero differences. Lowering tests 568, bir::j 58, none ignored. Trunk has not moved since the branch was cut.

Rulings

  1. The five lines the end-state search finds are not failures of this slice; the search in the prompt was too wide. args.rs:88, :124 push into Evaluation::statements, tail.rs:65 into tail_assignments, expr.rs:310 into the scratch of hold_effect, ffi_args.rs:305 into the string switch's actions. None is a call of the stack. The first four vectors are retyped by T6x-a2 (its task names each), the fifth by T6x-a4. The gap is the prompt's and is not counted against the builder, who stopped at the field boundary as the common block asks.
  2. The two row-to-vector-to-handle sequences (expr.rs:310-311, tail.rs:65/67) are accepted for the length of one slice. They are what "wrap at that place" produces while the vector between them is another slice's. T6x-a2 removes both; its review checks that they are gone.
  3. The empty-block table has two producers until T6x-a3, and that is accepted. place notes the row it was given; block_statement and the guard in free_effect note NodeId(js.stmt_id(..)) read before the push. The alternative was a turn-back pair at both sites, which the common block forbids. On rows the two ids are the same number, rows are the only implementation until T6v, and T6x-a3's task already rekeys the table by stmt_id at exactly these three places. The builder chose the form the next slice keeps.
  4. args.rs:43 builds a handle from a row to lend it to declares_pure and drops it. Accepted as a transition: T6x-a2 makes Evaluation::statements hold handles.

On record

  • No test drives a hold through take_list at 10,000 statements (take_run's inner loop and hold_run's move of an inner hold). The task asked for no new test. T6x-a2, which changes how a hold is filled, adds it.
  • The cost notes of note_empty_block and drop_empty_blocks name the binary searches and not the shift of the vector's tail on insert and drain. Trunk's notes had the same gap. T6x-a3 rewrites both functions and their notes.
  • Steps: js.list and release_stmt now count on rows where the pool push and Bir::release did not. No test reads the counter across a take_list; later step tests of the stack start from this.
  • Not rerun by me: the builder's local check-mode figure (-0.006%, five runs a side). The application-wide check-mode measurement is made once after the T6x-a slices.

@jfrolich
jfrolich merged commit 93bff1f into ocaml-to-rust-rewrite Nov 17, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant